﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class PhieuTraSachDAO
    {
        #region 1. Them
        public static bool ThemPhieuTraSach(PhieuTraSachDTO pts)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPT", pts.MaPT));
                sqlParams.Add(new SqlParameter("@NgayTra", pts.NgayTra));
                sqlParams.Add(new SqlParameter("@TongTienPhat", pts.TongTienPhat));
                sqlParams.Add(new SqlParameter("@SoThe", pts.SoThe));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertPHIEUTRA", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Sua
        public static bool SuaPhieuTraSach(PhieuTraSachDTO pts)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPT", pts.MaPT));
                sqlParams.Add(new SqlParameter("@NgayTra", pts.NgayTra));
                sqlParams.Add(new SqlParameter("@TongTienPhat", pts.TongTienPhat));
                sqlParams.Add(new SqlParameter("@SoThe", pts.SoThe));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdatePHIEUTRA", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaPhieuTraSach(String mapt)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPT", mapt));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeletePHIEUTRA", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static PhieuTraSachDTO TimPTS(String mapt)
        {
            PhieuTraSachDTO result = new PhieuTraSachDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPT", mapt));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUTRA", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaPT = dr["MaPT"].ToString();
                result.SoThe = dr["SoThe"].ToString();
                result.NgayTra = DateTime.Parse(dr["NgayTra"].ToString());
                result.TongTienPhat = decimal.Parse(dr["TongTienPhat"].ToString());
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaPhieuTraSach()
        {
            DataTable dt= new DataTable();
            try
            {
                    dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUTRAsAll");
                
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        public static bool CheckPTSByID(String mapt)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaPT", mapt));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectPHIEUTRA", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static string TimPhieuTraBangNguoiVaNgay(String manguoitra, DateTime ngaytra){
            string phieutra = "";
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@nguoitra", manguoitra));
                sqlParams.Add(new SqlParameter("@ngaytra", ngaytra));

                phieutra = SqlDataAccessHelper.ExecuteQuery("TimPhieuTraBangNguoiVaNgay", sqlParams).Rows[0]["MaPT"].ToString();
            }
            catch (System.Exception ex)
            {
            	
            }
            return phieutra;
        }
        #endregion
    }
}
